

import Box from "./Box";
import { CommonTipWnd } from "./CommonTipWnd";
import { BundleResInfo, CommonUtils } from "./CommonUtils";
import { EventManager } from "./EventManager";
import { EventType } from "./EventType";
import Execute from "./Execute";
import GlobaConfig from "./GlobaConfig";
import Shelf from "./Shelf";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Main extends cc.Component {

    @property(cc.Prefab) shelfPfb: cc.Prefab = null;
    @property(cc.Prefab) boxPfb: cc.Prefab = null;
    @property(cc.Node) content: cc.Node = null;
    @property(cc.Sprite) sprite: cc.Sprite = null;

    async start() {
        await Execute.GetInstance().initData()
        EventManager.Instance.on(EventType.FLY_BOX, this.creatorBox, this);
        // CommonTipWnd.Show({
        //     btnActiveNum: 1,
        //     infoStr: '测试弹窗',
        // });
        // this.spBrite.spriteFrame = await CommonUtils.GetInstance().getSpriteFrame('texture/GameItem/1');
        // 等待三秒后发送事件
        // await CommonUtils.GetInstance().sleepTime(3000);
        // EventManager.Instance.dispatch(EventType.TEST_EVENT);
        this.constructShelf()
    }

    // 创建仓库
    async constructShelf() {
        const jsonAsset = await Execute.GetInstance()._baseData;
        for (let i = 0; i < jsonAsset.shelf_num; i++) {
            const node = cc.instantiate(this.shelfPfb);
            this.content.addChild(node);
            this.content.getComponent(cc.Layout).spacingX = 1 * GlobaConfig.Pixels;
            node.width = jsonAsset.shelf_width * GlobaConfig.Pixels;
            node.getComponent(Shelf).init(jsonAsset, i)
        }
    }

    // 创建打包盒
    creatorBox(event){
        const node = cc.instantiate(this.boxPfb)
        node.width = event.box[4].width * GlobaConfig.Pixels;
        node.height = event.box[4].high * GlobaConfig.Pixels;
        this.putInShelf(node, event)
    }

    // 放入货架
    putInShelf(node, event){
        GlobaConfig.AllShelfCell[event.shelfNum][event.floorNum].addChild(node)
        node.setPosition(cc.v2(event.posY * 10, Math.floor(10 - event.posX) * 10))
        node.getComponent(Box).init(event)
    }

    // 拿走货物
    takeBox(event){
        GlobaConfig.AllShelfCell[event.shelfNum][event.floorNum].destroySelf();
    }

    // 更新一下货架物品
    updateShelfCell(event){

    }
}
